<!-- *Author: 黄旗, *Date: 2020-08-04 15:45:59 -->
<template>
    <Modal v-model='modal' :width="800" title="权限设置" :mask-closable='false' @on-cancel='canceledit'>
        <div v-for="(item, index) of powerkList" :key="item.value">
            <Divider v-if="index!=0"/>
            <h4 class="ivu-mb-8">
                <Checkbox
                :indeterminate="item.indeterminate"
                :value="item.checkAll"
                @click.prevent.native="handleCheckAll(item, index)">{{item.title}}</Checkbox>
            </h4>
            <CheckboxGroup v-model="modalparams.action_ids" @on-change="checkAllGroupChange(item)" class="ivu-ml-16">
                <template v-for="items of item.children">
                    <div :key="items.value" v-if="items.children&&items.children.length>0">
                        <Checkbox class="ivu-mb-8" :label="itemss.value" v-for="itemss of items.children" :key="itemss.value">{{itemss.title}}</Checkbox>
                    </div>
                    <Checkbox v-else :key="items.value" class="ivu-mb-8" :label="items.value">{{items.title}}</Checkbox>
                </template>
            </CheckboxGroup>
        </div>
        <div slot='footer'>
            <Button @click='canceledit'>取消</Button>
            <Button type='primary' @click='submitedit'>保存</Button>
        </div>
    </Modal>
</template>

<script>
    import { getRoleInfo, rolePreSet } from '@/api/powersetting.js';
    export default {
        name: '',
        components: {},

        data () {
            return {
                modal: false,
                modalparams: { id: 0, action_ids: [], menu_ids: [] },
                powerkList: []
            };
        },
        /* 函数方法集合 */
        methods: {
            /* @函数功能: 初始化 */
            initial (row) {
                this.modalparams = { id: row.id, action_ids: [], menu_ids: [] },
                this.modal = true
                this.askData(row)
            },
            askData (row) {
                getRoleInfo({ id: row.id, type: 1 }).then((res) => {
                    this.powerkList = res.auth
                    this.modalparams.action_ids = res.action_ids
                    this.modalparams.menu_ids = res.menu_ids
                })
            }, // 请求数据处理
            canceledit () {
                this.modal = false
            },
            submitedit () {
                this.modalparams.menu_ids = []
                for (let item of this.powerkList) {
                    for (let items of item.children) {
                        if (items.children) {
                            for (let itemss of items.children) {
                                let index = this.modalparams.action_ids.indexOf(itemss.value)
                                if (index >= 0) {
                                    this.modalparams.menu_ids.push(item.value)
                                    this.modalparams.menu_ids.push(items.value)
                                }
                            }
                        } else {
                            let indexs = this.modalparams.action_ids.indexOf(items.value)
                            if (indexs >= 0) this.modalparams.menu_ids.push(item.value)
                        }
                    }
                }
                let menu_ids = Array.from(new Set(this.modalparams.menu_ids))
                let params = {
                    id: this.modalparams.id,
                    action_ids: this.modalparams.action_ids.join(','),
                    menu_ids: menu_ids.join(',')
                }
                rolePreSet(params).then(() => {
                    this.$Message.success('授权成功')
                    this.modal = false
                })
            },
            handleCheckAll (item, index) {
                this.modalparams.action_ids = Array.from(new Set(this.modalparams.action_ids))
                if (item.indeterminate) {
                    item.checkAll = false;
                } else {
                    item.checkAll = !item.checkAll;
                }
                item.indeterminate = false;

                if (item.checkAll) {
                    for (let items of item.children) {
                        if (items.children) {
                            for (let itemss of items.children) {
                                this.modalparams.action_ids.push(itemss.value)
                            }
                        } else {
                            this.modalparams.action_ids.push(items.value)
                        }
                    }
                } else {
                    for (let items of item.children) {
                        if (items.children) {
                            for (let itemss of items.children) {
                                this.log(1, itemss.value, this.modalparams.action_ids)
                                let index = this.modalparams.action_ids.indexOf(itemss.value)
                                if (index >= 0) this.modalparams.action_ids.splice(index, 1)
                            }
                        } else {
                            let index = this.modalparams.action_ids.indexOf(items.value)
                            this.log(2, items.value, index)
                            if (index >= 0) this.modalparams.action_ids.splice(index, 1)
                        }
                    }
                }
            },
            checkAllGroupChange (item) {
                let indeterminate = false
                let checkAll = true
                let num = 0
                let keyvalue = []
                for (let items of item.children) {
                    if (items.children) {
                        for (let itemss of items.children) {
                            keyvalue.push(itemss.value)
                        }
                    } else {
                        keyvalue.push(items.value)
                    }
                }
                for (let item of keyvalue) {
                    if (this.modalparams.action_ids.indexOf(item) >= 0) {
                        indeterminate = true
                        num++
                    }
                    if (this.modalparams.action_ids.indexOf(item) < 0) checkAll = false
                }
                if (num == keyvalue.length) {
                    indeterminate = false
                }
                item.indeterminate = indeterminate
                item.checkAll = checkAll
            }
        }
    }
</script>
<style lang="less">
</style>
